<?php
/**
 *
 * 快钱支付接口
 * @author seiven
 *
 */
class Helper_Payment_Bill extends Helper_Payment_Abstract
{
	function __construct(){
		parent::__construct('bill');
	}
	/**
	 * 创建提交html
	 */
	function create_button()
	{
		$merchantAcctId = $this->_payconfig['mid'];// 商户编号
		$key =  $this->_payconfig['key'];
		$inputCharset = '1';//支付编码1=utf8 / 2=gbk / 3=gb2312
		$pageUrl = url('callback/pay',array('p_t'=>'bill'),null,array('base_uri'=>'http://'.trim($_SERVER['HTTP_HOST'])));// 前台支付结果显示页面
		$bgUrl = '';//url('callback/pay',array('p_t'=>'bill'),null,array('base_uri'=>'http://'.trim($_SERVER['HTTP_HOST'])));// 后台服务器结果通知
		$version = 'v2.0';// 版本
		$language = '1';// 支付页面显示
		$signType = '1'; // 加密方式
		$payerName = $this->_order['username']; // 支付人姓名
		$payerContactType = '1';// 支付人联系类型1=email
		$payerContact = $this->_order['useremail'];// 支付人联系方式
		$orderId = $this->_order['orderid'];//订单号长度小于30
		$orderAmount = intval($this->_order['money'] * 100);//订单金额  分
		$orderTime = date('YmdHis');//提交时间
		$productName = mb_substr(strip_tags($this->_order['good_title']),0,255,'UTF-8');//产品名称
		$productNum = '1';//产品数量
		$productId = $this->_order['good_id'] || '';//商品编号
		$productDesc = $this->_order['good_desc'] || '';//商品介绍
		$ext1 = $this->_order['ext1'] || '';//扩展1
		$ext2 = $this->_order['ext2'] || '';//扩展2
		$payType = '00';// 支付方式
		$bankId = '';//银行编码
		$redoFlag = '0';//订单处理机制
		$pid = '';//合作商户
		
		$sv = '';
		$sv = $this->appendParam($sv,'inputCharset',$inputCharset);
		$sv = $this->appendParam($sv,'pageUrl',$pageUrl);
		$sv = $this->appendParam($sv,'bgUrl',$bgUrl);
		$sv = $this->appendParam($sv,'version',$version);
		$sv = $this->appendParam($sv,'language',$language);
		$sv = $this->appendParam($sv,'signType',$signType);
		$sv = $this->appendParam($sv,'merchantAcctId',$merchantAcctId);
		$sv = $this->appendParam($sv,'payerName',$payerName);
		$sv = $this->appendParam($sv,'payerContactType',$payerContactType);
		$sv = $this->appendParam($sv,'payerContact',$payerContact);
		$sv = $this->appendParam($sv,'orderId',$orderId);
		$sv = $this->appendParam($sv,'orderAmount',$orderAmount);
		$sv = $this->appendParam($sv,'orderTime',$orderTime);
		$sv = $this->appendParam($sv,'productName',$productName);
		$sv = $this->appendParam($sv,'productNum',$productNum);
		$sv = $this->appendParam($sv,'productId',$productId);
		$sv = $this->appendParam($sv,'productDesc',$productDesc);
		$sv = $this->appendParam($sv,'ext1',$ext1);
		$sv = $this->appendParam($sv,'ext2',$ext2);
		$sv = $this->appendParam($sv,'payType',$payType);
		$sv = $this->appendParam($sv,'bankId',$bankId);
		$sv = $this->appendParam($sv,'redoFlag',$redoFlag);
		$sv = $this->appendParam($sv,'pid',$pid);
		$sv = $this->appendParam($sv,'key',$key);
		$signMsg= strtoupper($this->hash($sv, $signType));

		return "<form id='order-pay-form'  target='_self' method='post' action='https://sandbox2.99bill.com/gateway/recvMerchantInfoAction.htm'>
					<input type='hidden' name='inputCharset' value='{$inputCharset}'/>
					<input type='hidden' name='bgUrl' value='{$bgUrl}'/>
					<input type='hidden' name='pageUrl' value='{$pageUrl}'/>
					<input type='hidden' name='version' value='{$version}'/>
					<input type='hidden' name='language' value='{$language}'/>
					<input type='hidden' name='signType' value='{$signType}'/>
					<input type='hidden' name='signMsg' value='{$signMsg}'/>
					<input type='hidden' name='merchantAcctId' value='{$merchantAcctId}'/>
					<input type='hidden' name='payerName' value='{$payerName}'/>
					<input type='hidden' name='payerContactType' value='{$payerContactType}'/>
					<input type='hidden' name='payerContact' value='{$payerContact}'/>
					<input type='hidden' name='orderId' value='{$orderId}'/>
					<input type='hidden' name='orderAmount' value='{$orderAmount}'/>
					<input type='hidden' name='orderTime' value='{$orderTime}'/>
					<input type='hidden' name='productName' value='{$productName}'/>
					<input type='hidden' name='productNum' value='{$productNum}'/>
					<input type='hidden' name='productId' value='{$productId}'/>
					<input type='hidden' name='productDesc' value='{$productDesc}'/>
					<input type='hidden' name='ext1' value='{$ext1}'/>
					<input type='hidden' name='ext2' value='{$ext2}'/>
					<input type='hidden' name='payType' value='{$payType}'/>
					<input type='hidden' name='bankId' value='{$bankId}'/>
					<input type='hidden' name='redoFlag' value='{$redoFlag}'/>
					<input type='hidden' name='pid' value='{$pid}'/>
					<input type='image' src='".url('default')."images/pay/image/vouch_54.jpg' width='147' height='37' />
				</form>";
	}
	/**
	 * call back
	 */
	function callback()
	{
		$merchantAcctId = $this->_payconfig['mid'];// 商户编号
		$key =  $this->_payconfig['key'];
		$version = trim($_GET['version']);
		$language = trim($_GET['language']);
		$signType = trim($_GET['signType']);
		$payType = trim($_GET['payType']);
		$bankId = trim($_GET['bankId']);
		$orderId = trim($_GET['orderId']);
		$orderTime = trim($_GET['orderTime']);
		$orderAmount = trim($_GET['orderAmount']);
		$dealId = trim($_GET['dealId']);
		$bankDealId = trim($_GET['bankDealId']);
		$dealTime = trim($_GET['dealTime']);
		$payAmount = trim($_GET['payAmount']);
		$fee = trim($_GET['fee']);
		$ext1 = trim($_GET['ext1']);
		$ext2 = trim($_GET['ext2']);
		$payResult = trim($_GET['payResult']);
		$errCode = trim($_GET['errCode']);
		$signMsg = trim($_GET['signMsg']);
		//生成加密串。必须保持如下顺序。
		$merchantSignMsgVal = '';
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'merchantAcctId',$merchantAcctId);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'version',$version);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'language',$language);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'signType',$signType);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'payType',$payType);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'bankId',$bankId);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'orderId',$orderId);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'orderTime',$orderTime);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'orderAmount',$orderAmount);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'dealId',$dealId);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'bankDealId',$bankDealId);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'dealTime',$dealTime);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'payAmount',$payAmount);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'fee',$fee);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'ext1',$ext1);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'ext2',$ext2);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'payResult',$payResult);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'errCode',$errCode);
		$merchantSignMsgVal = $this->appendParam($merchantSignMsgVal,'key',$key);
		$merchantSignMsg = $this->hash($merchantSignMsgVal, $signType);
		//商家进行数据处理，并跳转会商家显示支付结果的页面
		///首先进行签名字符串验证
		if(strtoupper($signMsg) == strtoupper($merchantSignMsg)){
			switch($payResult){
				case '10':// 支付成功
					// 生成支付订单数组数据
					$this->back_order = array(
						'orderId'=>$orderId,
						'money'=>$payAmount,
						'ext1'=>$ext1,
						'ext2'=>$ext2,
						'bankId'=>$bankId
					);
					return true;
					break;
				default:
					$this->back_result = array(
						'message'=>'支付失败!',
						'result'=>$errCode
					);
					break;
			}
		}else{
			$this->back_result = array(
				'message'=>'订单信息被篡改',
				'result'=>10000,
			);
		}
		return false;
	}
	/**
	 *
	 * 加密方式
	 */
	function hash($string,$signType){
		switch ($signType)
		{
			case 4:
				return $this->get_pki_hash($string);
				break;
			case 1:
			default:
				return md5($string);
		}
	}
	/**
	 *
	 * pki 加密算法
	 */
	function get_pki_hash($string)
	{
		return md5($string);
	}
	/**
	 *
	 * 获取服务器通知静态返回
	 */
	function get_static_return($status,$param = array())
	{
		if ($status)
		{
			$r = "<result>1</result><redirecturl>{$param['url']}</redirecturl>";
		}else{
			$r = "<result>0</result><redirecturl>{$param['url']}</redirecturl>";
		}
		return $r;
	}
}